﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mono.Cecil;
using OpenReflect.Domain.Contracts;
using OpenReflect.Domain.Model;
using OpenReflect.Services.Builders;

namespace OpenReflect.Test.BuilderTests
{
	/// <summary>
	///This is a test class for ParameterBuilderTest and is intended
	///to contain all ParameterBuilderTest Unit Tests
	///</summary>
	[TestClass]
	public class ParameterBuilderTest
	{
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		private AssemblyDefinition Definition { get; set; }

		private TypeDefinition TDef
		{
			get { return Definition.MainModule.Types["OpenReflect.Test.TestBinaries.TestClass1"]; }
		}

		#region Additional test attributes

		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//

		#endregion

		[TestInitialize]
		public void MyTestInitialize()
		{
			Definition =
				AssemblyFactory.GetAssembly(@"OpenReflect.Test.TestBinaries.dll");
		}

		/// <summary>
		///A test for CreateParameter
		///</summary>
		[TestMethod]
		public void CreateParameterTest()
		{
			IParameterBuilder target = new ParameterBuilder(); // TODO: Initialize to an appropriate value
			MethodDefinition method = TestHelper.CreateMethodDefinition(TDef, "NormalAddMethod");
			ParameterDefinition definition = TestHelper.CreateParameterDefinition(method, "param1");
				// TODO: Initialize to an appropriate value

			IParameter actual = target.CreateParameter(definition);
			Assert.IsNotNull(actual);
		}

		/// <summary>
		///A test for CreateParameter
		///</summary>
		[TestMethod]
		public void should_name_return_correctly()
		{
			IParameterBuilder target = new ParameterBuilder(); // TODO: Initialize to an appropriate value
			MethodDefinition method = TestHelper.CreateMethodDefinition(TDef, "NormalAddMethod");
			ParameterDefinition definition = TestHelper.CreateParameterDefinition(method, "param1");
				// TODO: Initialize to an appropriate value
			IParameter actual = target.CreateParameter(definition);
			Assert.AreEqual(actual.Name, definition.Name);
		}


		//[TestMethod()]
		//public void FlowPlayTest()
		//{
		//    MethodDefinition method = TestHelper.CreateMethodDefinition(TDef, "NormalAddMethod");
		//    ControlFlowGraph cfg = FlowGraphFactory.CreateControlFlowGraph(method);
		//    foreach (InstructionBlock block in cfg.Blocks)
		//    {
		//        //Console.WriteLine(block.FirstInstruction.OpCode.);
		//        block.
		//    }
		//}

		/// <summary>
		///A test for CreateParameters
		///</summary>
		[TestMethod]
		public void CreateParametersTest()
		{

			var target = new ParameterBuilder();
			ModuleDefinition asmDef = AssemblyFactory.GetAssembly(typeof (string).Assembly.Location).MainModule;
			IMethodDeclarationBuilder methodDeclarationBuilder = new MethodDeclarationBuilder();
			TypeDefinition aDef = Definition.MainModule.Types["OpenReflect.Test.TestBinaries.TestClass1"];
			TypeDefinition stringDef = asmDef.Types["System.String"];
			TypeDefinition intDef = asmDef.Types["System.Int32"];
			MethodDefinition definition = TestHelper.CreateMethodDefinition(aDef, "AnAbstractMethod");
			ITypeDeclarationBuilder typeDeclarationBuilder = new TypeDeclarationBuilder();

			IParameterBuilder parameterBuilder=new ParameterBuilder();
			IList<IParameter> actual = target.CreateParameters(definition);
			Assert.AreEqual(2, actual.Count);
			Assert.AreEqual("param1",actual[0].Name);
			Assert.AreEqual("Int32", actual[0].TypeDeclaration.Name);
			Assert.AreEqual("param2", actual[1].Name);
			Assert.AreEqual("String", actual[1].TypeDeclaration.Name);
		}
	}
}